package keeto.com.vn.bms.ui.widget;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.activity.HomeBMSActivity;
import keeto.com.vn.bms.ui.activity.ListSeminarActivity;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class LogoutDialog extends Dialog implements android.view.View.OnClickListener  {

	private Context ctx;
	private Button btnOk;
	private Button btnCancel;
	private BMSSharedPreferences preferences;
	private Listenner mListener = new Listenner();
	private LinearLayout llParent;

	public LogoutDialog(Context context) {
		super(context);
		this.ctx = context;
		this.preferences = new BMSSharedPreferences(context);
	}


	protected LogoutDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		// TODO Auto-generated constructor stub
	}

	public LogoutDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.logout_dialog_layout);
		llParent = (LinearLayout)findViewById(R.id.parent_logout_dialog);
		btnOk = (Button)findViewById(R.id.btn_ok_logout);
		btnCancel = (Button)findViewById(R.id.btn_cancel_logout);
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		this.setCanceledOnTouchOutside(false);
		Rect displayRectangle = new Rect();
        Window window = this.getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
        llParent.setMinimumWidth((int) (displayRectangle.width()* 0.9f));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok_logout:
			BMSCore.getInstance(ctx).taskLogout(preferences.getUserAuthKey(ctx), mListener);
			preferences.clear();
			HomeBMSActivity.actionLaunch(ctx);
			((Activity) ctx).finish();
			LogoutDialog.this.dismiss();
			break;
		case R.id.btn_cancel_logout:
			this.dismiss();
			break;

		default:
			break;
		}
	}
	class Listenner extends BMSListener{
		@Override
		public void finishLogout(final int returnCode, final String msg) {
			// TODO Auto-generated method stub
			super.finishLogout(returnCode, msg);
			((Activity) ctx).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK ){
						preferences.clear();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ctx, ctx.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else {
						Toast.makeText(ctx, ctx.getResources().getString(R.string.error_logout), Toast.LENGTH_SHORT).show();
					}
				
				}
			});

		}
	}
}
